﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UrlChecker.Setting
{
    public partial class SearchList : Form
    {
        #region Attributes

        public List<string> SearchSelections = new List<string>();
        private List<string> SearchNames = new List<string>();
        private List<string> SearchUsing = new List<string>();

        private int ColumnsInterVal = 0;  //每列显示的间隔;
        private int ColumnsCount = 4;   //每行显示个数
        private int RowsInterVal = 10;    //每行显示的间隔

        #endregion

        public SearchList()
        {
            InitializeComponent();
        }

        private void SearchList_Load(object sender, EventArgs e)
        {
            DataTable dt = null;

            dt = QT.SEO.SearchEngine.GetAllList();
            CheckBox cbox = null;
            int LocationX = 25;
            int LocationY = 20;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cbox = new CheckBox();
                cbox.Name = dt.Rows[i]["sid"].ToString();
                cbox.Text = dt.Rows[i]["SearchName"].ToString();
                cbox.Location = new Point(LocationX, LocationY);
                cbox.Checked = false;
                cbox.Visible = true;
                cbox.Click += new EventHandler(cbox_Click);
                this.Controls.Add(cbox);
                if (SearchSelections.Contains(dt.Rows[i][0].ToString()))
                {
                    cbox.Checked = true;
                    SearchNames.Add(cbox.Text);
                    SearchUsing.Add(cbox.Name);
                }
                if (i % ColumnsCount == 0)
                {
                    this.Height += cbox.Size.Height + RowsInterVal;
                    this.btnOK.Location = new Point(this.btnOK.Location.X, this.btnOK.Location.Y + cbox.Size.Height + RowsInterVal);
                    this.btnClose.Location = new Point(this.btnClose.Location.X, this.btnClose.Location.Y + cbox.Size.Height + RowsInterVal);
                }
                LocationX += cbox.Size.Width + ColumnsInterVal;
                if (i % ColumnsCount == (ColumnsCount - 1))
                {
                    LocationX = 25;
                    LocationY += cbox.Size.Height + RowsInterVal;
                }
            }
        }

        public void cbox_Click(object sender, EventArgs e)
        {
            if (((CheckBox)sender).Checked)
            {
                SearchUsing.Add(((CheckBox)sender).Name);
                SearchNames.Add(((CheckBox)sender).Text);
            }
            else
            {
                SearchUsing.Remove(((CheckBox)sender).Name);
                SearchNames.Remove(((CheckBox)sender).Text);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            SearchRefresh(SearchUsing, SearchNames);
            this.Close();
        }

        public delegate void ButtonHanlder(List<string> SearchSelections, List<string> SearchNames);
        public event ButtonHanlder SearchRefresh;
    }
}
